package api

import (
	"context"
	// "fmt"
	"strconv"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/system/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetApiListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetApiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiListLogic {
	return &GetApiListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetApiListLogic) GetApiList(req *types.SystemApiListReq) (resp *types.SystemApiListResp, err error) {
	// logrus.Info(fmt.Sprintf("ApiListReq: %+v", req))

	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var ids *string
	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}

	data, err := l.svcCtx.BackendRpc.GetApiList(l.ctx, &backendclient.ApiListReq{
		Page:        page,
		PageSize:    pageSize,
		Ids:         ids,
		Path:        req.Path,
		Description: req.Description,
		Group:       req.Group,
		Method:      req.Method,
		Required:    req.Required,
		Status:      req.Status,
		StartTime:   req.StartTime,
		EndTime:     req.EndTime,
		OrderBy:     orderBys,
		FilterBy:    filterBys,
	})
	if err != nil {
		return nil, err
	}

	resp = &types.SystemApiListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.SystemApiListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}

	// Translate api group
	var group = make(map[string]string)
	for _, v := range data.List {
		// If page size is over 1000, use api group i18n translation. Mainly use in authority list.
		if pageSize == 0 {
			if _, ok := group[*v.Group]; !ok {
				group[*v.Group] = l.svcCtx.Trans.Trans(l.ctx, "apiGroup."+*v.Group)
			}
			*v.Group = group[*v.Group]
		}
		option := utils.GetOptionByStatus(v.Status)
		resp.Data.List = append(resp.Data.List, &types.SystemApiInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Path:        v.Path,
			Trans:       l.svcCtx.Trans.Trans(l.ctx, *v.Description),
			Description: v.Description,
			Group:       v.Group,
			Method:      v.Method,
			Required:    v.Required,
			Status:      v.Status,
		})
	}

	return resp, nil
}
